package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementAlarmBo;
import com.ruoyi.zhgd.domain.vo.NCementAlarmVo;
import com.ruoyi.zhgd.domain.NCementAlarm;
import com.ruoyi.zhgd.mapper.NCementAlarmMapper;
import com.ruoyi.zhgd.service.INCementAlarmService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 沥青拌合站筛孔数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementAlarmServiceImpl implements INCementAlarmService {

    private final NCementAlarmMapper baseMapper;

    /**
     * 查询沥青拌合站筛孔数据
     */
    @Override
    public NCementAlarmVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public TableDataInfo<NCementAlarmVo> queryPageList(NCementAlarmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementAlarm> lqw = buildQueryWrapper(bo);
        Page<NCementAlarmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public List<NCementAlarmVo> queryList(NCementAlarmBo bo) {
        LambdaQueryWrapper<NCementAlarm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementAlarm> buildQueryWrapper(NCementAlarmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementAlarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGravel1() != null, NCementAlarm::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NCementAlarm::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NCementAlarm::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NCementAlarm::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NCementAlarm::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NCementAlarm::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NCementAlarm::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NCementAlarm::getPowder2, bo.getPowder2());
        lqw.eq(bo.getWater() != null, NCementAlarm::getWater, bo.getWater());
        lqw.eq(bo.getAlarm() != null, NCementAlarm::getAlarm, bo.getAlarm());
        lqw.eq(StringUtils.isNotBlank(bo.getDetails()), NCementAlarm::getDetails, bo.getDetails());
        lqw.eq(bo.getState() != null, NCementAlarm::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmReason()), NCementAlarm::getAlarmReason, bo.getAlarmReason());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPlan()), NCementAlarm::getHandlerPlan, bo.getHandlerPlan());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerResult()), NCementAlarm::getHandlerResult, bo.getHandlerResult());
        lqw.eq(bo.getHandlerTime() != null, NCementAlarm::getHandlerTime, bo.getHandlerTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPerson()), NCementAlarm::getHandlerPerson, bo.getHandlerPerson());
        return lqw;
    }

    /**
     * 新增沥青拌合站筛孔数据
     */
    @Override
    public Boolean insertByBo(NCementAlarmBo bo) {
        NCementAlarm add = BeanUtil.toBean(bo, NCementAlarm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站筛孔数据
     */
    @Override
    public Boolean updateByBo(NCementAlarmBo bo) {
        NCementAlarm update = BeanUtil.toBean(bo, NCementAlarm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementAlarm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站筛孔数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取预警处置信息
     *
     * @param id
     * @return
     */
    @Override
    public NCementAlarmVo getHandlerById(String id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public void handlerUpdate(NCementAlarmBo bo) {
        if (ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("更新数据id不能为空");
        }
        NCementAlarm entity = new NCementAlarm();
        entity.setAlarmReason(bo.getAlarmReason());
        entity.setHandlerPlan(bo.getHandlerPlan());
        entity.setHandlerResult(bo.getHandlerResult());
        entity.setHandlerTime(bo.getHandlerTime());
        entity.setHandlerPerson(bo.getHandlerPerson());
        entity.setState(1L);
        entity.setId(bo.getId());
        baseMapper.updateById(entity);
    }
}
